background_mod = {
    cssId: 'backgroundCSS',
    css: "div.content{padding: 0 10px 0 10px !important;border:2px solid #D9D9D9 !important;border-top:none !important;background-color:white !important;} body{background-image:url('[url_image]');}",
    urlExp: /^(https?:\/\/(?:[a-z0-9]*\.)?[a-z0-9_\-]+\.[a-z0-9.]{2,})(?:\/[a-z0-9_ \-#=&?\/\.]*)?$/i,
    init: function() {
        config_mod.onsaveconfig.add(this);

        var cfg = tuenti_lib.config.get();
        this.changeBackground(cfg.background);
    },
    changeBackground: function(url) {
        var style = document.getElementById(this.cssId);

        if (!url || url === '' || url === 'http://') {
            if (style){
                system_lib.removeStyle(this.cssId);
            }
            return false;
        }

        url = unescape(url);

        if (!this.urlExp.test(url)) {
            console.error('background: la url es invalida: ' + url);
            return false;
        }

        var css = this.css.replace(/\[url_image]/gi, url);

        if (style) {
            style.textContent = css;
        }
        else {
            system_lib.addStyle(css , this.cssId);
        }
        
        return true;
    },
    onSaveConfig: function(cfg) {
        if (!this.changeBackground(cfg.background)) {
            cfg.background = 'http://';
        }
    }
};

background_mod.init();